package com.evl.contacts.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.evl.common.annotion.Permission;
import com.evl.common.annotion.log.BussinessLog;
import com.evl.common.constant.Dict;
import com.evl.common.constant.factory.PageFactory;
import com.evl.common.controller.BaseController;
import com.evl.common.exception.BizExceptionEnum;
import com.evl.common.exception.BussinessException;
import com.evl.common.persistence.dao.FrequentContactTMapper;
import com.evl.common.persistence.model.FrequentContactT;
import com.evl.core.log.LogObjectHolder;
import com.evl.core.util.ToolUtil;
import com.evl.core.vo.BaseResultVo;
import com.evl.core.vo.JsonReturnHelper;

/**
 * 常用联系人控制器
 *
 * @author 
 * @Date 2017-06-15 11:31:18
 */
@Controller
@RequestMapping("/api/contacts")
@Api(value = "用户联系人")
public class ContactsController extends BaseController {

    private String PREFIX = "/contacts/";

    @Autowired
    private FrequentContactTMapper frequentContactTMapper;
    
    /**
     * 跳转到常用联系人首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "contacts.html";
    }

    /**
     * 跳转到添加常用联系人
     */
    @RequestMapping("/contacts_add")
    public String contactsAdd() {
        return PREFIX + "contacts_add.html";
    }

    /**
     * 跳转到修改常用联系人
     */
    @RequestMapping("/contacts_update/{contactsId}")
    public String contactsUpdate(@PathVariable Integer contactsId, Model model) {
    	FrequentContactT contacts = frequentContactTMapper.selectById(contactsId);
        model.addAttribute("contacts" , contacts);
        LogObjectHolder.me().set(contacts);
    	return PREFIX + "contacts_edit.html";
    }

    /**
     * 获取常用联系人列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name) {
    	Page<FrequentContactT> page = new PageFactory<FrequentContactT>().defaultPage();
        List<FrequentContactT> result =  frequentContactTMapper.getFrequentContactTs(page, name);
        for(FrequentContactT contact : result){
        	if(contact.getGender().equals("1")){
        		contact.setGender("女");
        	}else{
        		contact.setGender("男");
        	}
        	if(contact.getCardType().equals("1")){
        		contact.setCardType("护照");
        	}else{
        		contact.setCardType("身份证");
        	}
        }
        page.setRecords(result);
        return super.packForBT(page);
    }

    /**
     * 新增常用联系人
     */
    @BussinessLog(value = "添加常用联系人", key = "simplename", dict = Dict.ContactsMap)
    @RequestMapping(value = "/add")
    @Permission
    @ResponseBody
    public Object add(FrequentContactT frequentContactT) {
    	 if (ToolUtil.isOneEmpty(frequentContactT, frequentContactT.getBirthdate(),frequentContactT.getCardNo(),frequentContactT.getName(),frequentContactT.getTelephone())) {
             throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
         }
         return this.frequentContactTMapper.insert(frequentContactT);
    }

    /**
     * 删除常用联系人
     */
    @BussinessLog(value = "删除常用联系人", key = "simplename", dict = Dict.ContactsMap)
    @Permission
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer contactsId) {
    	this.frequentContactTMapper.deleteById(contactsId);
        return SUCCESS_TIP;
    }


    /**
     * 修改常用联系人
     */
    @BussinessLog(value = "修改常用联系人", key = "simplename", dict = Dict.ContactsMap)
    @Permission
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(FrequentContactT frequentContactT) {
    	this.frequentContactTMapper.updateAllColumnById(frequentContactT);
        return super.SUCCESS_TIP;
    }

    /**
     * 常用联系人详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
    
   /**
    * 根据opendId查询联系人
    * @param openId
    * @return
    */
    @ResponseBody
    @ApiOperation(value="根据opendId查询联系人",httpMethod = "GET", response = FrequentContactT.class, notes = "根据opendId查询联系人")
    @RequestMapping(value = "/getFrequentContactListByOpendId",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public BaseResultVo getFrequentContactListByOpendId(@ApiParam(required = true, name = "opendId", value = "根据opendId查询联系人") @RequestParam("opendId") String opendId){
    	
    	
    	List<FrequentContactT> list=frequentContactTMapper.getFrequentContactListByOpendId(opendId);
    	return JsonReturnHelper.getSuccessWithData(list);
    	
    }
    /**
     * 根据联系人id查询联系人
     * @param id
     * @return
     */
    @ResponseBody
    @ApiOperation(value="根据联系人id查询联系人",httpMethod = "GET", response = FrequentContactT.class, notes = "根据联系人id查询联系人")
    @RequestMapping(value = "/getFrequentContactById",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public BaseResultVo getFrequentContactById(@ApiParam(required = true, name = "id", value = "根据联系人id查询联系人") @RequestParam("id") String id){
		
    	FrequentContactT frequentContact = frequentContactTMapper.getFrequentContactById(id);
    	return JsonReturnHelper.getSuccessWithData(frequentContact);
    	
    }
    
    /**
     * 用户添加联系人
     * @param frequentContactVO
     */
    @ResponseBody
    @ApiOperation(value="用户添加联系人",httpMethod = "POST", response = FrequentContactT.class, notes = "用户添加联系人")
    @RequestMapping(value = "/addFrequentContact",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    public void addFrequentContact(@ApiParam(required = true, name = "frequentContactVO", value = "用户添加联系人") @RequestBody FrequentContactT frequentContactVO){
    	frequentContactTMapper.addFrequentContact(frequentContactVO);
    }
    
     /**
      * 用户修改联系人
      * @param frequentContactVO
      */
    @ApiOperation(value="用户修改联系人",httpMethod = "POST", response = FrequentContactT.class, notes = "用户修改联系人")
    @RequestMapping(value = "/updateFrequentContact",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody
    public void updateFrequentContact(@ApiParam(required = true, name = "frequentContactVO", value = "用户修改联系人") @RequestBody FrequentContactT frequentContactVO){
    	frequentContactTMapper.updateFrequentContact(frequentContactVO);
    }
    
    /**
     * 根据联系人ID删除联系人
     * @param id
     */
  /*  @ApiOperation(value="根据联系人ID删除联系人",httpMethod = "POST", response = FrequentContactT.class, notes = "根据联系人ID删除联系人")
    @RequestMapping(value = "/delFrequentContactById",method = RequestMethod.POST,produces = "application/json; charset=utf-8")
    @ResponseBody*/
    
    @ResponseBody
    @ApiOperation(value="根据联系人ID删除联系人",httpMethod = "GET", response = FrequentContactT.class, notes = "根据联系人ID删除联系人")
    @RequestMapping(value = "/delFrequentContactById",method = RequestMethod.GET,produces = "application/json; charset=utf-8")
    public void delFrequentContactById(@ApiParam(required = true, name = "id", value = "根据联系人ID删除联系人") @RequestParam Integer id){
    	frequentContactTMapper.delFrequentContactById(id);
    }
}
